package xq.gwt.ui.widgets.command;
import com.smartgwt.client.widgets.form.fields.LinkItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;

import xq.gwt.mvc.controller.CommandController;
import xq.gwt.mvc.model.CommandModel;
import xq.gwt.mvc.view.CommandView;
import xq.gwt.mvc.view.ObservableViewImp;
import xq.gwt.ui.widgets.feedback.DefaultExceptionPresenter;
import xq.gwt.ui.widgets.feedback.DefaultFeedbackPresenter;

public class LinkItemCommandPresenter extends ObservableViewImp implements CommandView{
	protected CommandController controller;
	protected LinkItem link;
	
	public LinkItemCommandPresenter(){
		link = new LinkItem();
		init();
	}
	
	public LinkItemCommandPresenter(LinkItem link){
		this.link = link;
		init();
	}

	private void init(){
		link.addClickHandler(new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				notifyViewChangedListeners();				
			}
		});
		link.setShowTitle(false);
		
		controller = new CommandController(this);
	}
	
	@Override
	public void setEnabled(boolean enabled) {
		if(enabled)
			link.enable();
		else
			link.disable();
		
	}

	@Override
	public void showFeedback(String feedback) {
		DefaultFeedbackPresenter presenter = new DefaultFeedbackPresenter();
		presenter.showFeedback(feedback);
		
	}

	@Override
	public void reportException(Exception ex) {
		DefaultExceptionPresenter exPresenter = new DefaultExceptionPresenter();
		exPresenter.showException(ex);
		
	}

	@Override
	public void executeStarted() {
		link.disable();
		
	}

	@Override
	public void excuteFinished() {
		link.enable();
		
	}

	@Override
	public void setCaption(String caption) {
		link.setValue(caption);
		
	}
	
	public LinkItem getLink() {
		return link;
	}
	
	public void setModel(CommandModel model){
		controller.setCommandModel(model);
	}
}
